public class Work5 {
    public static int state = 0;
    public static final Object locker = new Object();

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            cbaPrint(Thread.currentThread().getName(),2);
        },"a");
        Thread t2 = new Thread(() -> {
            cbaPrint(Thread.currentThread().getName(),1);
        },"b");
        Thread t3 = new Thread(() -> {
            cbaPrint(Thread.currentThread().getName(),0);
        },"c");
        t1.start();
        t2.start();
        t3.start();
    }
    public static void cbaPrint(String name,int targetState) {
        synchronized (locker) {
            while (state % 3 != targetState) {
                try {
                    locker.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.print(name);
            //state == targetState
            state++;
            locker.notifyAll();
        }
    }
}
